using Crux.Core.Dapper.SqlGenerator;

namespace Crux.Core.Dapper;
public partial class QueryRepository<TContext> where TContext : DapperContext
{
    /// <inheritdoc/>
    public TEntity? Find<TEntity>(
        object id,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null,
        params Expression<Func<TEntity, object>>[] includes) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetSelectById(id, null, ignoreFilter: ignoreFilter, includes: includes);

        return QueryFirstOrDefault<TEntity>(sqlQuery.GetSql(), sqlQuery.Param);
    }
    /// <inheritdoc/>
    public async Task<TEntity?> FindAsync<TEntity>(
        object id,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null,
        CancellationToken cancellationToken = default,
        params Expression<Func<TEntity, object>>[] includes) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetSelectById(id, null, ignoreFilter: ignoreFilter, includes: includes);

        return await QueryFirstOrDefaultAsync<TEntity>(sqlQuery.GetSql(), sqlQuery.Param, cancellationToken).ConfigureAwait(false);
    }

}